package service;

import common.Message;
import common.MessageType;
import dao.GetLearn;
import dao.GetTeach;

import java.io.ObjectOutputStream;
import java.util.Iterator;

public class FlushCourse {
    //更新全部在线成员
    public static void flush(){
        Message ms=new Message();
        ms.setState(MessageType.send_Course);
        Iterator<String> iterator = ControlSCConnectionThread.getHashMap().keySet().iterator();
        //遍历所有在线用户，然后去更新课程列表
        while (iterator.hasNext()) {
            String key = iterator.next();
            flush(key);
        }
    }
    //更新指定在线成员
    public static void flush(String key){
        ServerConnectionClient value = ControlSCConnectionThread.getHashMap().get(key);
        if(value==null){
            System.out.println(key+"离线");
            return;
        }
        System.out.println("刷新"+key);
        try{
            ObjectOutputStream  oos=new ObjectOutputStream(value.getSocket().getOutputStream());
            Message ms=new Message();
            ms.setState(MessageType.send_Course);
            oos.writeObject(ms);
            oos.writeObject(GetTeach.getTeach(key));//我教的课的集合
            oos.writeObject(GetLearn.getLearn(key));//我学的课的集合
        }catch (Exception e){
            e.printStackTrace();
            System.out.println(key+"拉取用户课程失败");
        }
    }
}
